package com.wswy.wzcx.ui.car.choose

import android.support.v4.content.ContextCompat
import android.support.v7.widget.RecyclerView
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Space
import android.widget.TextView
import com.blankj.utilcode.util.SizeUtils
import com.che.libcommon.ui.adapter.SampleRecyclerViewAdapter
import com.google.android.flexbox.FlexboxLayout
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter
import com.wswy.wzcx.R
import com.wswy.wzcx.model.ICity
import com.wswy.wzcx.model.WZCity
import com.wswy.wzcx.model.car.JZGCity
import com.wswy.wzcx.model.resp.ProvinceData
import com.wswy.wzcx.module.base.gone
import com.wswy.wzcx.module.base.loadLayout
import com.wswy.wzcx.module.base.visible
import com.wswy.wzcx.widget.HotCityLayout
import kotlinx.android.synthetic.main.layout_list_item_city.view.*

class ChooseCityAdapter(val type:Int) : SampleRecyclerViewAdapter<ICity,ChooseCityAdapter.ViewHolder>(), StickyRecyclerHeadersAdapter<ChooseCityAdapter.HeaderViewHolder> {

    companion object {

        const val HOT_TYPE_WZ = 1
        const val HOT_TYPE_JZG = 2
        const val SHOW_TYPE_P_CITY = 3

        private const val TYPE_HOT = 1
        private const val TYPE_CITY = 2

        private const val TYPE_PROVINCE_CITY = 3
    }

    init {

        if (type == SHOW_TYPE_P_CITY){
            setOnItemClickListener { _, position, t ->
                if (position != expandPosition){
                    //
                    val old = expandPosition
                    expandPosition = position
                    if (old != -1){
                        notifyItemChanged(old)
                    }
                    notifyItemChanged(expandPosition)
                }
            }
        }
    }

    var topHotClickListener : ((ICity)->Unit)?=null

    var cityClickListener : ((ICity)->Unit)?=null
    private var expandPosition = -1

    override fun getHeaderId(position: Int): Long {
        if (position == 0) return -1
        return getItemData(position)?.getHeaderId()?:0
    }

    override fun onCreateHeaderViewHolder(parent: ViewGroup?): HeaderViewHolder {
        return HeaderViewHolder(LayoutInflater.from(parent?.context).inflate(R.layout.layout_item_car_header,parent,false))
    }

    override fun onBindHeaderViewHolder(holder: HeaderViewHolder?, position: Int) {
        holder?.tvTitle?.text = getItemData(position)?.nameLatter
    }

    override fun itemClickable(position: Int): Boolean {
        return position != 0 || type != SHOW_TYPE_P_CITY;
    }

    override fun getItemViewType(position: Int): Int {

        if (position == 0){
            return TYPE_HOT
        }else{
            if (type == SHOW_TYPE_P_CITY){
                return TYPE_PROVINCE_CITY
            }else{

                return TYPE_CITY
            }
        }

        //return if (position == 0) TYPE_HOT else TYPE_CITY
    }



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val inflater = LayoutInflater.from(parent.context)

         return when(viewType){
            TYPE_HOT -> HotViewHolder(inflater.inflate(R.layout.item_layout_hot_city,parent,false),type,datas,topHotClickListener)
            TYPE_CITY -> ViewHolder(inflater.inflate(R.layout.layout_list_item_city,parent,false))
             TYPE_PROVINCE_CITY -> ProvCityViewHolder(loadLayout(parent,R.layout.layout_list_item_prov_city),cityClickListener)
             else -> ViewHolder(inflater.inflate(R.layout.layout_list_item_city,parent,false))
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int, data: ICity?) {
        holder.itemView.tv_name?.text = data?.cityName

        if (holder is ProvCityViewHolder && data is ProvinceData){
            if (expandPosition == position){
                holder.bindCity(data.list)
            }else{
                holder.close()
            }

        }

    }


    open class ViewHolder(itemView:View) : RecyclerView.ViewHolder (itemView){

    }


    class HotViewHolder(itemView: View,val type:Int, val dataList:List<ICity>, topHotClickListener:((ICity)->Unit)?):ViewHolder(itemView){


        init {

            val cities = if (type == HOT_TYPE_JZG) arrayListOf<ICity>(JZGCity("201","北京","B"),
                    JZGCity("2401","上海","S"),
                    JZGCity("501","广州","G"),
                    JZGCity("502","深圳","S"),
                    JZGCity("1501","南京","N"),
                    JZGCity("1502","苏州","S"))

            else arrayListOf<ICity>(WZCity("北京","beijing","110100000000",1,"北京"),
                    WZCity("上海","shanghai","310100000000",3,"上海"),
                    WZCity("广州","guangzhou","440100000000",19,"广东"),
                    WZCity("深圳","shenzhen","440300000000",19,"广东"),
                    WZCity("南京","nanjing","320100000000",10,"江苏"),
                    WZCity("苏州","suzhou","320500000000",10,"江苏"))

            (itemView as? HotCityLayout)?.let {

                it.topHotClickListener = topHotClickListener
                it.useLocalFind = type == SHOW_TYPE_P_CITY
                it.setHotCity(cities,dataList)
            }

        }

    }



    class HeaderViewHolder (itemView:View): RecyclerView.ViewHolder(itemView){
        var tvTitle: TextView = itemView as TextView

    }

    //省市
    class ProvCityViewHolder(itemView:View,private val cityClickListener:((ICity)->Unit)?) : ViewHolder (itemView),View.OnClickListener{

        private val layoutContent = itemView.findViewById<FlexboxLayout>(R.id.layout_city)
        private val imgArrow = itemView.findViewById<ImageView>(R.id.img_arrow)

        var isExpand = false
        private set

        override fun onClick(v: View?) {

            (v?.tag as? ICity)?.let {
                cityClickListener?.invoke(it)
            }
        }



        fun bindCity(list: List<ICity>){

            isExpand = true

            layoutContent.visible()
            imgArrow.rotation = 90f

            layoutContent.removeAllViews()

            val context = itemView.context

            val colors = ContextCompat.getColorStateList(context,R.color.press_textview_color)
            val max = 3
            val height = SizeUtils.dp2px(34f)
            val margin = SizeUtils.dp2px(10f)

            list.forEachIndexed { index, city ->

                TextView(context).apply {
                    textSize = 14f
                    text = city.cityName

                    setTextColor(colors)
                    setBackgroundResource(R.drawable.btn_choose_city_bg)
                    gravity = Gravity.CENTER

                    isClickable = true
                    tag = city
                    setOnClickListener(this@ProvCityViewHolder)

                }.run {
                    FlexboxLayout.LayoutParams(0, height).apply {

                        flexGrow = 1f

                        rightMargin = margin
                        if (index >= max){
                            topMargin = margin
                            if (index % max == 0){
                                isWrapBefore = true
                            }
                        }
                    }.let {
                        layoutContent.addView(this,it)
                    }

                }

            }

            val remain = list.size % max

            if (remain > 0 ){
                layoutContent.addView(Space(context), FlexboxLayout.LayoutParams(0, height).apply {
                    flexGrow = (max - remain).toFloat()

                    rightMargin = SizeUtils.dp2px(10f) * (max-remain)
                })
            }

        }

        fun close(){
            isExpand = false

            imgArrow.rotation =0f
            layoutContent.removeAllViews()
            layoutContent.gone()
        }

    }
}